package com.CountDownTatch;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

/**
 * Created by apple on 17/9/29.
 */
public class ApplicationStartHealth {
    private static List<BaseHealth> list_baseHealth;
    private static CountDownLatch cdl;

    public ApplicationStartHealth() {
    }

    private final static ApplicationStartHealth instance = new ApplicationStartHealth();

    public static ApplicationStartHealth getInstance() {
        return instance;
    }

    public static boolean checkHealth() throws InterruptedException {
        cdl = new CountDownLatch(1);

        list_baseHealth = new ArrayList<BaseHealth>();
        list_baseHealth.add(new DataBaseHealth(cdl));

//        Executor executor = Executors.newFixedThreadPool(list_baseHealth.size());

        ExecutorService es = Executors.newFixedThreadPool(3);
        List<Future<Boolean>> lsit_future = new ArrayList<Future<Boolean>>();
        for (BaseHealth bh: list_baseHealth
             ) {
            Future<Boolean> future = es.submit(bh);
            lsit_future.add(future);
//            executor.execute(bh);
        }

        cdl.await();
        es.shutdown();
        for (Future<Boolean> f: lsit_future
             ) {
            try {
                if(!f.get()){
                    return false;
                }
            } catch (ExecutionException e) {
                e.printStackTrace();
                return false;
            }
        }

        return true;
    }

    public static void main(String[] args) {
        boolean flag;
        try {
            flag = ApplicationStartHealth.checkHealth();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println();
    }
}
